<!-- Darija Dasic 97/08-->
<!-- Sofija Puric 6/08 -->

<?php include_once dirname(__FILE__) . '/staticStars.php'; ?>
<div id="hostels">
    <table align="right">
        <tr>
            <td><p> Sort by &nbsp;</p></td>
            <td><form action="hostels.php" method="post">

                    <select name="sort" size="1">
                        <option value="0">Select...</option>
                        <option value="1">Alphabetical</option>
                        <option value="2">Price descending</option>
                        <option value="3">Prices ascending</option>
                        <option value="4">Score descending</option>
                        <option value="5">Score ascending</option>
                    </select>
                    <button type="submit">Go!</button>
                </form>
            </td>
        </tr>
    </table>

    <?php
    require 'pages/db.php';
    $con = dbConnect("sib");


    function writeStars($HostelID) {
        $query = "SELECT (avg(Security)+avg(Location)+avg(Service)+avg(Fun)+avg(Cleanliness)+avg(Price))/6 AS prosek " .
                "FROM feedback WHERE IDHostel = $HostelID;";
        $conn = dbConnect("sib");
        $result = mysql_query($query, $conn);
        $row = mysql_fetch_array($result);
        $prosek = $row["prosek"];
        $ss = new staticStars(round($prosek));
        return $ss->printStars($prosek);
    }

    $rank = false;
    if (isset($_POST["sort"]) && !empty($_POST["sort"])) {

        $opt = $_POST["sort"];
        switch ($opt) {
            case 1: $sql_sort = "SELECT * FROM hostel ORDER BY Name";
                break;
            case 2: $sql_sort = "Select * FROM hostel ORDER BY PriceMax DESC";
                break;
            case 3: $sql_sort = "Select * FROM hostel ORDER BY PriceMin";
                break;
            case 4: {

                    $sql_sort = "SELECT hostel.IDHostel, hostel.Name, hostel.Description,
                        (rank_security + rank_location + rank_service + rank_fun + rank_cleanliness + rank_price)/6 AS rank_all
                        FROM hostelrank RIGHT JOIN hostel ON hostelrank.IDHostel = hostel.IDHostel ORDER BY rank_all DESC";
                    
                }
                break;
            case 5: {

                    $sql_sort = "SELECT hostel.IDHostel, hostel.Name, hostel.Description,
                        (rank_security + rank_location + rank_service + rank_fun + rank_cleanliness + rank_price)/6 AS rank_all
                        FROM hostelrank RIGHT JOIN hostel ON hostelrank.IDHostel = hostel.IDHostel ORDER BY rank_all ";
                    
                }
                break;
            default: $sql_sort = "SELECT * FROM hostel ORDER BY Name";
                break;
        }
    }
    else
        $sql_sort="SELECT * FROM hostel ORDER BY Name";
    $result = mysql_query($sql_sort, $con);
    if (!$result) {
        die('Error: ' . mysql_error());
    } else {
        while ($row = mysql_fetch_array($result)) {
            $idh = $row["IDHostel"];
            $sql_photo = "SELECT * FROM photo WHERE IDHostel = '$idh' AND IndexPhoto = '1'";
            $result_photo = mysql_query($sql_photo, $con);
            if (!$result_photo)
                die('Error: ' . mysql_error());
            $row_photo = mysql_fetch_array($result_photo);
            echo '<div class="hostel">
		<div class="title"><h2>' . $row["Name"] . '</h2></div>
		<a href = "hostel.php?hid=' . $idh . '"><img src="' . $row_photo["Url"] . '" alt="' . $row_photo["Alt"] . '"/></a>
                    <div class="rate_choice hostels_stars">';
            echo writeStars($idh);
            echo '  </div>		
                    <div class="entry">' . $row["Description"] . '</div>';
            //$idh = $row["IDHostel"];
            echo '
                <div class="links"><a href="hostel.php?hid=' . $idh . '">See more</a></div>
                
	</div>';
        }
    }
    mysql_close($con);
    ?>
</div>
